package cn.young.javalearning.designpattern.strategy.simpleStrategy.pro;

import cn.young.javalearning.designpattern.strategy.simpleStrategy.Shopping;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ShoppingStrategyFactory {

    private ShoppingStrategyFactory(){}

    private static final Map<ShoppingStrategy, Shopping> map = new ConcurrentHashMap<>(8);

    public static Shopping getInstance(ShoppingStrategy shoppingStrategy) throws Exception {
        Shopping shopping = map.get(shoppingStrategy);
        if(null == shopping){
            Class<?> aClass = Class.forName("cn.young.javalearning.designpattern.strategy.simpleStrategy." + shoppingStrategy.getShoppingStrategy());
            shopping = (Shopping) aClass.newInstance();
            map.put(shoppingStrategy, shopping);
        }
        return shopping;
    }

    public enum ShoppingStrategy {
       CAISHICHANG_STRATEGY("菜市场", "CaiShiChangStrategy"),
       SHANGCHUANG_STRATEGY("商场", "ShangChangStrategy");
       private String name;
       private String shoppingStrategy;
        public ShoppingStrategy setShoppingStrategy(String shoppingStrategy) {
            this.shoppingStrategy = shoppingStrategy;
            return this;
        }
        public String getName() {
           return name;
       }

       public String getShoppingStrategy() {
           return shoppingStrategy;
       }

       ShoppingStrategy(String name, String shoppingStrategy) {
           this.name = name;
           this.shoppingStrategy = shoppingStrategy;
       }
   }
}
